//
//  QNWebProgressView.m
//  QingNiu
//
//  Created by sumeng on 2017/12/28.
//  Copyright © 2017年 Yolanda. All rights reserved.
//

#import "QNWebProgressView.h"

@interface QNWebProgressView()<CAAnimationDelegate>

@property (nonatomic, strong) CALayer * maskLayer;

@end

@implementation QNWebProgressView

- (instancetype)initWithFrame:(CGRect )frame{
    if ([super initWithFrame:frame]){
        CAGradientLayer * layer = (CAGradientLayer *)[self layer];
        [layer setStartPoint:CGPointMake(0.0, 0.5)];
        [layer setEndPoint:CGPointMake(1.0, 0.5)];

        NSMutableArray *colors = [NSMutableArray array];
        for (NSInteger hue = 0; hue <= 360; hue += 5) {
            UIColor * color = [UIColor colorWithHue:1.0 * hue / 360
                                         saturation:1.0
                                         brightness:1.0
                                              alpha:1.0];
            [colors addObject:(id)[color CGColor]];
        }
        [layer setColors:[NSArray arrayWithArray:colors]];
        
        self.maskLayer = [CALayer layer];
        [self.maskLayer setFrame:CGRectMake(0.0, 0.0, 0.0, frame.size.height)];
        [self.maskLayer setBackgroundColor:[[UIColor blackColor] CGColor]];
        [layer setMask:self.maskLayer];
    }
    return self;
}

+ (Class)layerClass {
    return [CAGradientLayer class];
}

- (void)setProgress:(CGFloat)value {
    if (_progress != value){
        _progress = MIN(1.0, fabs(value));
        [self setNeedsLayout];
    }
}

- (void)layoutSubviews {
    CGRect maskRect = [self.maskLayer frame];
    maskRect.size.width = CGRectGetWidth([self bounds]) * self.progress;
    [self.maskLayer setFrame:maskRect];
}
@end
